dojo.provide("lotex.report.AbstractReport");
dojo.require("lotex.array");

lotex.report.AbstractReport = {
    _classnames: [],
    initClassFromDOM: function(dom) {
        var css = dom.className;
        this._classnames = !dojo.string.isBlank(css) ? css.split(" ") : [];
    },

    addClassName: function(css) {
        if(lotex.array.has(this._classnames,css)) return;
        this._classnames.push(css);
    },

    getClassStr: function() {
        if(this._classnames.length>0)
            return ' class="'+this._classnames.join(' ')+'"';
        return '';
    },

    getConfig: function(update) {
        if(dj_undef("__config",this) || update){
            this.__config = this.readConfig();
        }
        return this.__config;
    },

    /**
      Get config from classname.
      config string format: __name value__

      TODO refactor config string;
      @param name String: config item name.
      @param noName Any: the value when can't find name in config string.
      @param noValue Any: the value when the name no value in config string.

      @return  The value of this config item.
    */
    getConfigItem: function(name, noName, noValue) {
        if (!noName) noName = false;
        if (!noValue) noValue = true;
        var s = this._classnames.join(" ");
        var re = new RegExp("__"+name+"(\\s+([\\w-]+)__)*", "i");
        var r = s.match(re);
        return r == null
                ? noName
                : (r[2] == "" ? noValue : r[2]);
    },

    delConfigItem: function(name) {
        if(!name) return null;
        var re = new RegExp("__"+name+"(\\s+([\\w-]+)__)*", "i");
        var s = this._classnames.join(" ");
        s = s.replace(re, "");
        this._classnames = !dojo.string.isBlank(s) ? s.split(" ") : [];
    },

    setConfigItem: function(name, value) {
        if(!name) return null;
        this.delConfigItem(name);
        this._classnames.push("__"+name);
        if(value) this._classnames.push(value+"__");
    }
}